<?php
!defined("EW_ROOT") && exit('No direct script access allowed');
/**
* Ecommerce Web Content Management System
* @copyright (c) 2011 ewcms team http://www.ewcms.org
* @license  GNU GENERAL PUBLIC LICENSE (GPLv3)
* @author iceweb
*/
class init
{
	private function __construct(){}
	
	/**
	 * 
	 * 过滤输入，如果php.ini有开启转义则去掉其自动转义
	 */
	public static function initInput()
	{
		if(isset($_GET['GLOBALS'])) unset($_GET['GLOBALS']);
		if(isset($_POST['GLOBALS'])) unset($_POST['GLOBALS']);
		if(isset($_COOKIE['GLOBALS'])) unset($_COOKIE['GLOBALS']);
		if(isset($_FILES['GLOBALS'])) unset($_FILES['GLOBALS']);
				
		if(self::getGPC())
		{
			$_GET = self::daddslashes($_GET);
			$_POST = self::daddslashes($_POST);
			$_COOKIE = self::daddslashes($_COOKIE);
		}
	}
	
	/**
	 * 
	 * 获取运行环境的自动转义
	 */
	public static function getGPC()
	{
		return function_exists('get_magic_quotes_gpc') ?get_magic_quotes_gpc() :0;
	}
	
	/**
	 * 去掉字符的转义
	 * @param array|string $string
	 */
	public static function daddslashes(&$string)
	{
		return is_array($string) ? array_map(array('init','daddslashes'), $string) : stripslashes($string);
	}
	
	/**
	 * 设置输出的头信息 默认为：text/html 文档
	 * @param string $type
	 */
	public static function setHeader($type="text/html")
	{
		header("Content-type:".$type."; charset:".CHARSET);
	}
	
	/**
	 * 获取语言选项
	 */
	public static function getUserLang()
	{
		$lang=isset($_GET['lang']) ?$_GET['lang'] :false;
		if(!$lang)
		{
			$lang=isset($_COOKIE['ewLang']) ?$_COOKIE['ewLang'] :false;
		}
		else
		{
			setcookie("ewLang",$lang, time()+3600*24*7);
		}
		if($lang && preg_match("/^[a-zA-z]+$/i", $lang))
		{
			return $lang;
		}
		return false;
	}
}
